/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.peng.commdit.rest;

import me.peng.annotation.Log;
import me.peng.commdit.domain.CommodityOrder;
import me.peng.commdit.service.CommodityOrderService;
import me.peng.commdit.service.dto.CommodityOrderQueryCriteria;
import me.peng.domain.RecyclerReserve;
import me.peng.domain.config.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import me.peng.annotation.rest.AnonymousDeleteMapping;
import me.peng.annotation.rest.AnonymousGetMapping;
import me.peng.annotation.rest.AnonymousPostMapping;
import me.peng.annotation.rest.AnonymousPutMapping;
/**
 * @website https://el-admin.vip
 * @author pengjianye
 * @date 2021-10-03
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "api/commditOrder管理")
@RequestMapping("/api/commodityOrder")
public class CommodityOrderController {

    private final CommodityOrderService commodityOrderService;

    @Autowired
    CommodityOrderService commodityOrderService2;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('commodityOrder:list')")
    public void download(HttpServletResponse response, CommodityOrderQueryCriteria criteria) throws IOException {
        commodityOrderService.download(commodityOrderService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询api/commditOrder")
    @ApiOperation("查询api/commditOrder")
    @PreAuthorize("@el.check('commodityOrder:list')")
    public ResponseEntity<Object> query(CommodityOrderQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(commodityOrderService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增api/commditOrder")
    @ApiOperation("新增api/commditOrder")
    @PreAuthorize("@el.check('commodityOrder:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody CommodityOrder resources){
        return new ResponseEntity<>(commodityOrderService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改api/commditOrder")
    @ApiOperation("修改api/commditOrder")
    @PreAuthorize("@el.check('commodityOrder:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody CommodityOrder resources){
        commodityOrderService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除api/commditOrder")
    @ApiOperation("删除api/commditOrder")
    @PreAuthorize("@el.check('commodityOrder:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Integer[] ids) {
        commodityOrderService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    /**
     * 根据用户id查询用户所有订单
     */
    @AnonymousGetMapping("/getCommdityOrder/{parentId}")
    public SysResult getCommdityOrder(@PathVariable  Integer parentId){
        return  commodityOrderService2.getCommdityOrder(parentId);
    }
    /**
     * 根据用户订单号查询用户当前订单
     */
    @AnonymousGetMapping("/getOrdernumber/{Ordernumber}")
    public SysResult getOrdernumber(@PathVariable  String Ordernumber){
        return  commodityOrderService2.getOrdernumber(Ordernumber);
    }


    /**
     * 提交订单
     */
    @AnonymousPostMapping("/postCommdityOrder")
    public SysResult postCommdityOrder(@RequestBody CommodityOrder commodityOrder){
        return  commodityOrderService2.postCommdityOrder(commodityOrder);
    }

    /**
     * 删除订单
     */
    @AnonymousDeleteMapping("/deleteCommdityOrder/{id}")
    public SysResult deleteCommdityOrder(@PathVariable  Integer id){

        return  commodityOrderService2.deleteCommdityOrder(id);
    }

    /**
     * 修改订单状态
     */
    @AnonymousPutMapping("/putCommdityOrder")
    public SysResult putOrderInfo(@RequestBody CommodityOrder commodityOrder){

        return commodityOrderService2.putCommdityOrder(commodityOrder);
    }





}
